package com.wjk.kylin.modules.system.schedule.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wjk.kylin.core.common.aspect.Log;
import com.wjk.kylin.core.common.controller.BaseController;
import com.wjk.kylin.core.common.data.RequestData;
import com.wjk.kylin.core.common.data.ResponseData;
import static com.wjk.kylin.core.constant.CommonConstant.BusinessType.*;

import com.wjk.kylin.core.common.valid.insert;
import com.wjk.kylin.core.common.valid.update;
import com.wjk.kylin.core.utils.PageUtils;
import com.wjk.kylin.modules.system.schedule.entity.Schedule;
import com.wjk.kylin.modules.system.schedule.service.IScheduleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;



/**
 * <p>
 *  定时任务 前端控制器
 * </p>
 *
 * @author wangjkui
 * @since 2020-06-04
 */
@Api("定时任务")
@RestController
@RequestMapping("/sys/schedule")
public class ScheduleController extends BaseController {

    @Autowired
    private IScheduleService scheduleService;

    @PreAuthorize("hasAuthority('system:schedule:list')")
    @ApiOperation("列表")
    @Log(title = "定时任务-列表",businessType = LIST,isSaveResponseData = false)
    @GetMapping("/list")
    public ResponseData list(Schedule schedule) {
        IPage<Schedule> page = scheduleService.findPage(new RequestData<Schedule>(), schedule);
        return ResponseData.success().data("page", new PageUtils(page));
    }

    @PreAuthorize("hasAuthority('system:schedule:query')")
    @ApiOperation("详情")
    @Log(title = "定时任务-详情",businessType = GET)
    @GetMapping("/{scheduleId}")
    public ResponseData get(@PathVariable String scheduleId) {
        Schedule schedule = scheduleService.getById(scheduleId);
        return ResponseData.success().data("schedule", schedule);
    }

    @PreAuthorize("hasAuthority('system:schedule:add')")
    @ApiOperation("新增")
    @Log(title = "定时任务-新增",businessType = INSERT)
    @PostMapping
    public ResponseData save(@Validated(insert.class) @RequestBody Schedule schedule){
        boolean flag = scheduleService.save(schedule);
        if (flag) {
            return ResponseData.success();
        } else {
            return ResponseData.error("新增失败");
        }
    }

    @PreAuthorize("hasAuthority('system:schedule:edit')")
    @ApiOperation("编辑")
    @Log(title = "定时任务-编辑",businessType = UPDATE)
    @PutMapping
    public ResponseData update(@Validated(update.class) @RequestBody Schedule schedule) {
        boolean flag = scheduleService.update(schedule);
        if (flag) {
            return ResponseData.success();
        } else {
            return ResponseData.error("编辑失败");
        }
    }

    @PreAuthorize("hasAuthority('system:schedule:delete')")
    @ApiOperation("删除")
    @Log(title = "定时任务-删除",businessType = DELETE)
    @DeleteMapping("/{scheduleIds}")
    public ResponseData delete(@PathVariable String[] scheduleIds) {
        boolean flag = scheduleService.deleteByIds(scheduleIds);
        if (flag) {
            return ResponseData.success();
        } else {
            return ResponseData.error("删除失败");
        }
    }


    @PreAuthorize("hasAuthority('system:schedule:edit')")
    @ApiOperation("暂停/恢复")
    @Log(title = "定时任务-暂停/恢复",businessType = UPDATE)
    @PatchMapping("/status/{scheduleId}/{status}")
    public ResponseData changeStatus(@PathVariable String scheduleId,@PathVariable String status) {
        boolean flag = scheduleService.changeStatus(scheduleId,status);
        if (flag) {
            return ResponseData.success();
        } else {
            return ResponseData.error("操作失败");
        }
    }

    @PreAuthorize("hasAuthority('system:schedule:edit')")
    @ApiOperation("立即执行一次")
    @Log(title = "定时任务-触发")
    @PatchMapping("/run/{scheduleId}")
    public ResponseData run(@PathVariable String scheduleId) {
        scheduleService.run(scheduleId);
        return ResponseData.success();
    }


}
